package nowcoder.offer;

import java.util.LinkedList;

/**
 * 圆圈中最后剩下的数
 *
 * @author briarbear
 * @blog https://briarbear.github.io
 * @date 2018-08-28 15:54
 */
public class LastRemaining {

    public int solution(int n,int m){
        if (n == 0 || m == 0) return -1;
        if (n  == 1) return 0;
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < n; list.add(i),i++);
        int index = 0;
        while (list.size() > 1){
            int size = list.size();
            index = (index+m-1) % size;
            list.remove(index);
        }

        return list.get(0);
    }

    public static void main(String[] args) {
        new LastRemaining().solution(5,4);
    }
}
